package yl.hs.bmipfull.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
import yl.hs.bmipfull.security.AuthenticationFilter;
import yl.hs.bmipfull.security.AuthenticationProvider;
import yl.hs.bmipfull.security.SecurityAccessDeniedHandler;
import yl.hs.bmipfull.security.SecurityAuthenticationEntryPoint;
import yl.hs.bmipfull.service.production.IProductionRecordService;
import yl.hs.bmipfull.service.system.IOrganizationInfoService;
import yl.hs.bmipfull.utils.HsApp;

/**
 * @author 华砂信息 <br/>
 * Web权限配置
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Qualifier("userServiceImpl")
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private IOrganizationInfoService organizationInfoService;
    @Autowired
    private IProductionRecordService productionRecordService;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 使用自定义登录身份认证组
        auth.authenticationProvider(new AuthenticationProvider(userDetailsService,organizationInfoService));
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.cors().and().csrf().disable()
                .exceptionHandling().authenticationEntryPoint(new SecurityAuthenticationEntryPoint())
                .accessDeniedHandler(new SecurityAccessDeniedHandler()).and()
                .authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .antMatchers("/largescale/*").permitAll()
                .antMatchers("/photos/*").permitAll()
                .antMatchers("/files/*").permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/systemIsInit").permitAll()
                .antMatchers("/saveInit").permitAll()
                .antMatchers("/construction/login").permitAll()
                .antMatchers("/construction/WechatLogin").permitAll()
                .antMatchers("/swagger**/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/v2/**").permitAll()
                .antMatchers("/doc.html").permitAll()
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/api-doc/**").permitAll()
                .antMatchers("/host/**").permitAll()
                .antMatchers("/jmreport/desreport_/**").permitAll()
                .antMatchers("/registration/**").permitAll()
                .antMatchers("/HSApi/**").permitAll()
                .antMatchers("/RemoteOrder/**").permitAll()
                .antMatchers("/api/productShipOrder/image/*").permitAll()
                .anyRequest().authenticated();

        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        // 退出登录处理器
        http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
        // 开启登录认证流程过滤器
        // http.addFilterBefore(new LoginFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);

        // 访问控制时登录状态检查过滤器
        http.addFilterBefore(new AuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
        //super.configure(http);

    }

    @Bean
    @Override
    public AuthenticationManager authenticationManager() throws Exception {

        return super.authenticationManager();
    }

}
